package com.tuanbiao.ui.auth;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.abiao.lib_utils.ViewUtils;
import com.hzjq.data.minbao.entity.AuthEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.AuthController;
import com.tuanbiao.databinding.ActivityAuthListBinding;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.events.AuthListEvent;
import com.tuanbiao.models.AuthModel;
import com.tuanbiao.presenter.auth.AuthListPresenter;
import com.tuanbiao.ui.auth.adapter.AuthListAdapter;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.utils.LibCollections;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class AuthListActivity extends BasePresenterActivity<AuthListPresenter, ActivityAuthListBinding> implements AuthController.ListView {
    private AuthListAdapter authListAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, AuthListActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityAuthListBinding getBindingView() {
        return ActivityAuthListBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.srlContent.setEnableLoadMore(false);
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        authListAdapter = new AuthListAdapter();
        authListAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            AuthEntity entity = (AuthEntity) adapter.getItem(position);
            EventBus.getDefault().postSticky(new AuthListEvent(entity));
            AuthDetailsActivity.start(getActivity());
        });
        authListAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            showDeleteDialog((AuthEntity) adapter.getItem(position));
            return true;
        });
        binding.rvContent.setAdapter(authListAdapter);
        binding.srlContent.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.refresh();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.loadMore();
            }
        });
    }

    private void showDeleteDialog(AuthEntity item) {
        new MessageDialog.Builder(getActivity())
                .setTitle(R.string.common_note)
                .setMessage("确定删除当前授权记录吗？")
                .setListener(dialog -> mPresenter.deleteAuth(item))
                .show();
    }


    @Override
    public void setItems(List<AuthEntity> entities) {
        if (LibCollections.isEmpty(entities)) {
            authListAdapter.setEmptyView(ViewUtils.layoutId2View(R.layout.include_no_data));
        }
        authListAdapter.setNewInstance(entities);
    }

    @Override
    public void addItems(List<AuthEntity> entities) {
        if (LibCollections.isEmpty(entities)) {
            binding.srlContent.finishLoadMoreWithNoMoreData();
        } else {
            authListAdapter.addData(entities);
        }
    }

    @Override
    protected AuthListPresenter createPresenter() {
        return new AuthListPresenter(new AuthModel(), this);
    }
}